﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YiLe.DAL.Dao;
using YiLe.Model.Domain;
using NHibernate;
using NHibernate.Criterion;

namespace YiLe.DAL.NHibernate
{
    public class UserDaoHibernate : BaseHibernate<User, int>, UserDao
    {

        public User Get(string loginname, string loginpwd)
        {
            IList<User> userList = HibernateTemplate.Find<User>("from User u where u.LoginName = ? and u.LoginPWD = ?", new Object[]{loginname, loginpwd});
            if (userList.Count == 0)
            {
                return null;
            }
            else
            {
                return userList[0];
            }
        }


        public IList<User> Search(User user)
        {
            ICriteria criteria = Session.CreateCriteria<User>();
            criteria.Add(Restrictions.Like("UserName", "%" + user.UserName + "%"));
            criteria.AddOrder(new Order("UserID", true));
            //IList<User> userList = HibernateTemplate.Find<User>("from User u");
            //return userList;
            return criteria.List<User>();
        }
    }
}
